// pages/mine/pages/my/afterSales/afterSales.js
const util = require('../../../../../utils/util');
const request = require('../../../../../utils/request')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    page:1,
    navbarHeight: 0,
    limit:10,
    isLastPage:false,
    currentTab: 0,
    navlist: [{
        name: '待取件',
        type: 'aftersale_qujian'
      },
      {
        name: '已完成',
        type: 'aftersale_qujianed'
      }
    ],
    status: 'aftersale_qujian',
    isShowReason: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      navbarHeight: util.getNavbarHeight(),
      userInfo: wx.getStorageSync('userInfo')
    })
    this.getList()
  },
  toBack() {
    wx.navigateBack()
  },
  tabClick(e) {
    const index = e.currentTarget.dataset.index;
    let item = e.currentTarget.dataset.item;
    this.setData({
      currentTab: index,
      status: item.type,
      page: 1,
      list: [],
      isLastPage: false,
    })
    this.getList()
  },
  showMore(e) {
    console.log(e)

  },
  submit(e) {
    let data = e.detail
    let _this = this
    data.order_id = this.data.order_id
    request._get('/api?method=shequ.complete', data).then(res => {
        getApp().toast(res.msg)
        _this.setData({
            list: [],
            page: 1,
            isLastPage: false,
        })
        setTimeout(function () {
          _this.getList()
        }, 1000)
    })
  },
  closeReason(){
    this.setData({
      isShowReason: !this.data.isShowReason
    })
  },
  // 操作订单
  handleOrder(e) {
    let type = e.currentTarget.dataset.type
    let item = e.currentTarget.dataset.item
    this.setData({
      selectOrder: item,
    })
    if (type == 1) { //取消订单
      this.setData({
        isShowReason: true,
      })
    } else if (type == 2) { //删除订单
      this.delOrderApi()
    } else if (type == 3) { //提醒发货
      this.remindSend()
    } else if (type == 4) { //延长收货
      this.lengthenOrder()
    } else if (type == 5) { //再次购买
      this.reBuy()
    } else if (type == 6) { //申请售后
      // this.sureGetOrder()

    }

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },
  sureSend(e) {
    let data = e.detail
    let _this = this
    data.order_id = this.data.order_id
    request._post('/api?method=shequ.reject', data).then(res => {
      getApp().toast('提交成功')
      setTimeout(function(){
        _this.setData({
          list:[],
          page:1,
          isLastPage:false,
          isShowReason: false
        })
        _this.getList()
      },1000)
    })
  },
  showPopup(e){
    let order_id = e.currentTarget.dataset.item.order_id
    this.setData({
      order_id:order_id,
      isShowReason: !this.data.isShowReason
    })
  },
  closeReason(){
    this.setData({
      isShowReason: !this.data.isShowReason
    })
  },
  qujian(e) {
    console.log(e)
    // 取件
    let _this = this
    let order_id = e.currentTarget.dataset.item.order_id
    request._post('/api?method=shequ.qujian', {
      order_id
    }).then(res => {
      getApp().toast('取件成功')
      setTimeout(function () {
        _this.setData({
          list: [],
          page: 1,
          isLastPage: false
        })
        _this.getList()
      }, 1000)
    })
  },
  getList() {
    let _this = this
    request._post('/api?method=shequ.saleList', {
      page: this.data.page,
      limit: this.data.limit,
      status: this.data.status
    }).then(res => {
      if (res.data.list.length == 0 || res.data.list == null) {
        _this.setData({
          isLastPage: true,
        })
      }
      _this.setData({
        list: _this.data.list.concat(res.data.list)
      })
     
    })
  },
  // 上拉加载
  onReachBottom: function () {
    if (this.data.isLastPage) {
      return
    }
    let page = this.data.page
    page++
    this.setData({
      page,
    })
    this.getList()
  },
  onDetails(e) {
    let id = e.currentTarget.dataset.id
    let item = e.currentTarget.dataset.item
    if (item.order_type == 1) {
        //普通订单
        wx.navigateTo({
            url: '/pages/mine/pages/my/order-details/order-details?id=' + id,
        })
    } else {
        // 拼团订单
        wx.navigateTo({
            url: '/pages/home/pages/my/group-details/group-details?id=' + id,
        })
    }
},
})